package mysql

import (
	"gorm.io/gorm"
	"video-manager/usersrpc/usersrpc"
)

type Collection struct {
	gorm.Model
	Video_id       int    `gorm:"type:int(11);commit:'视频ID'"`
	User_id        int    `gorm:"type:int(11);commit:'用户ID'"`
	Collect_time   string `gorm:"type:varchar(300);commit:'收藏时间'"`
	Collect_status int8   `gorm:"type:tinyint;default:0;commit:'收藏状态'"`
}

func (c *Collection) CollectionAdd(db *gorm.DB, userId int64, videoId int64) error {
	return db.Debug().Where("collections.user_id= ?", userId).Where("collections.video_id=?", videoId).Create(c).Error
}

func (c *Collection) FindCollectionById(db *gorm.DB, userId int64, videoId int64) error {
	return db.Debug().Where("collections.user_id= ?", userId).Where("collections.video_id=?", videoId).First(c).Error
}

func (c *Collection) CollectionDel(db *gorm.DB, id int64) error {
	return db.Debug().Where("collections.id=?", id).Delete(c).Error
}

func (c *Collection) FindCollectionByCId(db *gorm.DB, id int64) error {
	return db.Debug().Where("collections.id=?", id).First(&c).Error
}
func CollectionList(db *gorm.DB, in *usersrpc.CollectionListRequest, list []*usersrpc.CollectionList) []*usersrpc.CollectionList {
	db.Debug().Model(&Collection{}).Select("videos.video_title AS video_title",
		"users.nick_name as nick_name",
		"videos.cover_link AS cover_link",
		"videos.play_num as play_num").
		Joins("JOIN videos ON videos.id = collections.video_id").
		Joins("JOIN users ON users.id = collections.user_id").
		Order("collections.created_at DESC").
		Scopes(Paginate(int(in.Page), int(in.Size))).Find(&list)
	return list
}
